package com.hfb.mashibing.alip8.juc.c020;


import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 可重入锁
 */
public class T02_ReentrantLock2 {
    Lock lock = new ReentrantLock();

    void m () {
        try {
            lock.lock();
            for (int i = 0; i < 10; i++) {
                TimeUnit.SECONDS.sleep(2);

                System.out.println(i);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }  finally {
            lock.unlock();
        }
    }
    void n() {
        try {
            lock.lock();
            System.out.println("n。。。");

        } finally { lock.unlock();}
    }

    public static void main(String[] args) {
        T02_ReentrantLock2 lock = new T02_ReentrantLock2();
        new Thread(lock::m).start();

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(lock::n).start();

    }

}
